package com.imti.controller;

import com.imti.model.Clazz;

import com.imti.model.dto.ClazzUser;
import com.imti.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @RequestMapping("/clazz")
    public String findAllClazz(Clazz clazz, HttpServletRequest request){
        List<Clazz> clazzList = clazzService.findAllClazz(clazz);
        request.setAttribute("clazzList",clazzList);
        return "clazz/clazz-list";
    }

    @RequestMapping("/addClazz")
    public String addClazz(){
        return "clazz/clazz-add";
    }
    @RequestMapping("/add")
    public String addClazzById(Clazz clazz){
        clazzService.addClazzById(clazz);
        return "redirect:/clazzs/clazz";
    }
    @RequestMapping("/deleteClazz/{id}")
    public String deleteClazz(@PathVariable("id")Integer id){
        clazzService.deleteClazz(id);
        return "redirect:/clazzs/clazz";
    }


    @RequestMapping("/selectClazz/{id}")
    public String selectClazz(@PathVariable("id")Integer id, Model model){
        List<ClazzUser> clazzUserList = clazzService.selectClazz(id);
        model.addAttribute("list",clazzUserList);
        return "clazz/clazzUser";
    }
    @RequestMapping("/select")
    public String select(ClazzUser clazz){
        clazzService.select(clazz);
        return "redirect:/clazzs/clazz";
    }
}
